package cn.lds.health.controller;

import cn.lds.health.dto.AppointMentRecordCondition;
import cn.lds.health.dto.Pager;
import cn.lds.health.dto.ResultDTO;
import cn.lds.health.entity.AppointmentRecord;
import cn.lds.health.entity.User;
import cn.lds.health.exception.SysException;
import cn.lds.health.service.AppointmentRecordService;
import cn.lds.health.service.UserService;
import cn.lds.health.service.impl.AppointmentRecordServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @ClassName AppointmentRecordController
 * @Description TODO
 * @Author Christopher
 * @Date 2023/8/9 08:52
 * @Version 1.0
 **/

@Controller
@RequestMapping("/appointment_record")
public class AppointmentRecordController {
	
	@Autowired
	private AppointmentRecordService appointmentRecordService = new AppointmentRecordServiceImpl();
	
	@Autowired
	private UserService userService;
	private Logger log = LoggerFactory.getLogger(AppointmentRecordController.class);

	@RequestMapping("/appoint_list")
	public String appointList(AppointMentRecordCondition condition,Pager pager,Model model) throws SysException {
		ResultDTO<List<AppointmentRecord>> listResultDTO = appointmentRecordService.selectAppointMentRecordByCondition(condition, pager);
		model.addAttribute("result",listResultDTO);
		return "appoint_list";
	}
	
	/**
	 * 显示我的预约记录
	 * @param session
	 * @param model
	 * @return
	 * @throws SysException
	 */
	@RequestMapping("/my_appoint")
	public String myAppoint(HttpSession session, Model model) throws SysException {
		User loginUser = (User) session.getAttribute("user");

		AppointMentRecordCondition condition = new AppointMentRecordCondition();
		condition.setUname(loginUser.getName());
		
		ResultDTO<List<AppointmentRecord>> listResultDTO = appointmentRecordService.selectAppointMentRecordByCondition(condition, new Pager());
		
		model.addAttribute("result",listResultDTO);
		
		return "my_appoint";
		
	}
	
	/**
	 * 确认预约
	 * @param id
	 * @return
	 * @throws SysException
	 */
	@RequestMapping("/confirm/{id}")
	public String confirmAppoint(@PathVariable Integer id) throws SysException {
		ResultDTO resultDTO = appointmentRecordService.confirmAppoint(id);
		return "redirect:/appointment_record/appoint_list";
	}
	
	/**
	 * 取消体检
	 * @param id
	 * @return
	 * @throws SysException
	 */
	@RequestMapping("/cancel_appoint/{id}")
	public String cancelAppoint(@PathVariable Integer id) throws SysException {
		ResultDTO resultDTO = appointmentRecordService.cancelAppoint(id);
		return "redirect:/appointment_record/my_appoint";
	}
	
	/**
	 * 前往提交体检页面
	 * @param userId
	 * @param packageId
	 * @param model
	 * @return
	 * @throws SysException
	 */
	@RequestMapping("/appoint/{userId}/{packageId}")
	public String appoint(@PathVariable Long userId,@PathVariable Integer packageId,Model model) throws SysException {
		
		log.info("userId:{}",userId);
		
		ResultDTO<User> userResultDTO = userService.selectUserById(userId);
		
		log.info("user:{}",userResultDTO.getData());
		
		model.addAttribute("loginUser",userResultDTO.getData());
		model.addAttribute("packageId",packageId);
		
		return "appoint";
	}
	
	/**
	 * 接收表单提交内容，保存数据
	 * @param ar
	 * @param model
	 * @return
	 * @throws SysException
	 */
	@RequestMapping("/do_appoint")
	public String doAppoint(AppointmentRecord ar,Model model) throws SysException {
		//设置数据插入时间
		ar.setCreateTime(new Date());
		ResultDTO resultDTO = appointmentRecordService.addAppointment(ar);
		
		model.addAttribute("result",resultDTO);
		return "add_success";
	}
}